/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.nodes;

import com.googlecode.ivybeans.facade.IvyLibrary;
import com.googlecode.ivybeans.module.TextValueCompleter;
import com.googlecode.ivybeans.module.resources.ResourcesAccess;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;

/**
 *
 * @author lforet
 */
public class AddLibraryPanel extends javax.swing.JPanel {

    private JButton okButton;
    private TextValueCompleter orgCompleter;
    private TextValueCompleter nameCompleter;
    private TextValueCompleter revisionCompleter;
    private TextValueCompleter confCompleter;
    private final static List<String> EMPTY_VALUES = Collections.emptyList();
    private final IvyLibrary ivyLibrary;

    /** Creates new form AddLibraryPanel */
    public AddLibraryPanel(IvyLibrary ivyLibrary) {
        initComponents();
        this.ivyLibrary = ivyLibrary;
        okButton = new JButton(NbBundle.getMessage(ResourcesAccess.class, "BTN_OK"));

        orgCompleter = new TextValueCompleter(EMPTY_VALUES, orgTxt);
        nameCompleter = new TextValueCompleter(EMPTY_VALUES, nameTxt);
        revisionCompleter = new TextValueCompleter(EMPTY_VALUES, revisionTxt);
        confCompleter = new TextValueCompleter(EMPTY_VALUES , confTxt, "->");

//        orgTxt.addFocusListener(new FocusAdapter() {
//
//            @Override
//            public void focusGained(FocusEvent e) {
                orgCompleter.setValueList(EMPTY_VALUES);
                orgCompleter.setLoading(true);
                RequestProcessor.getDefault().post(new Runnable() {

                    public void run() {
                        populateOrgs();
                    }
                });
//            }
//        });
        nameTxt.addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent e) {
                nameCompleter.setValueList(EMPTY_VALUES);
                nameCompleter.setLoading(true);
                RequestProcessor.getDefault().post(new Runnable() {

                    public void run() {
                        populateNames();
                    }
                });
            }
        });
        revisionTxt.addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent e) {
                revisionCompleter.setValueList(EMPTY_VALUES);
                revisionCompleter.setLoading(true);
                RequestProcessor.getDefault().post(new Runnable() {

                    public void run() {
                        populateRevisions();
                    }
                });
            }
        });
        confTxt.addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent e) {
                confCompleter.setValueList(EMPTY_VALUES);
                confCompleter.setLoading(true);
                RequestProcessor.getDefault().post(new Runnable() {

                    public void run() {
                        populateConfigurations();
                    }
                });
            }
        });


    }

    public JButton getOkButton() {
        return okButton;
    }

    public String getOrg() {
        return orgTxt.getText().trim();
    }

    public String getLibraryName() {
        return nameTxt.getText().trim();
    }

    public String getRevision() {
        return revisionTxt.getText().trim();
    }

    public String getConf() {
        return confTxt.getText().trim();
    }

    private void populateOrgs() {
        assert !SwingUtilities.isEventDispatchThread();
        final Set<String> lst = new TreeSet<String>(ivyLibrary.listOrganizations());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                orgCompleter.setValueList(lst);
                orgCompleter.setLoading(false);
            }
        });
    }

    private void populateNames() {
        assert !SwingUtilities.isEventDispatchThread();
        final Set<String> lst = new TreeSet<String>(ivyLibrary.listModules(orgTxt.getText().trim()));
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                nameCompleter.setValueList(lst);
                nameCompleter.setLoading(false);
            }
        });
    }

    private void populateRevisions() {
        assert !SwingUtilities.isEventDispatchThread();
        final Set<String> lst = new TreeSet<String>(ivyLibrary.listRevisions(orgTxt.getText().trim(), nameTxt.getText().trim()));
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                revisionCompleter.setValueList(lst);
                revisionCompleter.setLoading(false);
            }
        });
    }

    private void populateConfigurations() {
        assert !SwingUtilities.isEventDispatchThread();
        Set<String> set = new TreeSet<String>();
        set.add("compile");
        set.add("runtime");
        final Set<String> lst = new TreeSet<String>(ivyLibrary.listConfigurations());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                confCompleter.setValueList(lst);
                confCompleter.setLoading(false);
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        orgLbl = new javax.swing.JLabel();
        nameLbl = new javax.swing.JLabel();
        revisionLbl = new javax.swing.JLabel();
        confLbl = new javax.swing.JLabel();
        orgTxt = new javax.swing.JTextField();
        nameTxt = new javax.swing.JTextField();
        revisionTxt = new javax.swing.JTextField();
        confTxt = new javax.swing.JTextField();

        orgLbl.setText(org.openide.util.NbBundle.getMessage(AddLibraryPanel.class, "AddLibraryPanel.orgLbl.text")); // NOI18N

        nameLbl.setText(org.openide.util.NbBundle.getMessage(AddLibraryPanel.class, "AddLibraryPanel.nameLbl.text")); // NOI18N

        revisionLbl.setText(org.openide.util.NbBundle.getMessage(AddLibraryPanel.class, "AddLibraryPanel.revisionLbl.text")); // NOI18N

        confLbl.setText(org.openide.util.NbBundle.getMessage(AddLibraryPanel.class, "AddLibraryPanel.confLbl.text")); // NOI18N

        orgTxt.setText(org.openide.util.NbBundle.getMessage(AddLibraryPanel.class, "AddLibraryPanel.orgTxt.text")); // NOI18N

        nameTxt.setText(org.openide.util.NbBundle.getMessage(AddLibraryPanel.class, "AddLibraryPanel.nameTxt.text")); // NOI18N

        revisionTxt.setText(org.openide.util.NbBundle.getMessage(AddLibraryPanel.class, "AddLibraryPanel.revisionTxt.text")); // NOI18N

        confTxt.setText(org.openide.util.NbBundle.getMessage(AddLibraryPanel.class, "AddLibraryPanel.confTxt.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(confLbl)
                    .add(revisionLbl)
                    .add(nameLbl)
                    .add(orgLbl))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(confTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .add(revisionTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 129, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(nameTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .add(orgTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(orgLbl)
                    .add(orgTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLbl)
                    .add(nameTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(revisionLbl)
                    .add(revisionTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(confLbl)
                    .add(confTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(14, 14, 14))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel confLbl;
    private javax.swing.JTextField confTxt;
    private javax.swing.JLabel nameLbl;
    private javax.swing.JTextField nameTxt;
    private javax.swing.JLabel orgLbl;
    private javax.swing.JTextField orgTxt;
    private javax.swing.JLabel revisionLbl;
    private javax.swing.JTextField revisionTxt;
    // End of variables declaration//GEN-END:variables
}
